﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Channels;
using Big2.Big2WcfService;
using Big2.CardsLib;
using Big2.Big2Game;
using System.Threading;

namespace Big2.Big2WcfService
{
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
    public class Server 
    {
        ServiceHost serviceHost;
        Thread thread;
        public delegate void ServerStartedEventHandler();
        public event ServerStartedEventHandler ServerStarted;
        public Connect Connection { get; set; }
        public Big2ServerService Big2Server;


        public Server()
        {
            InitServer();
        }

        public void InitServer()
        {
            Big2Server = new Big2ServerService();
            serviceHost = new ServiceHost(typeof(Big2ServerService));
        }
        
        public void startServer()
        {
            thread = new Thread(runServer);
            thread.IsBackground = true;
            thread.Start();
        }
        public void runServer()
        {
            #if debug
            System.Windows.MessageBox.Show("starting server..");
            #endif            
            //serviceHost.AddServiceEndpoint(typeof(IBig2ServerService), new NetTcpBinding(), "net.tcp://localhost:8010/Big2ServerService");
            serviceHost.Open();
            if (ServerStarted != null)
                ServerStarted();
            #if debug
            System.Windows.MessageBox.Show("server started..");
            #endif
        }
        public void CloseServer()
        {
            try
            {
                if (serviceHost.State == CommunicationState.Opened)
                    serviceHost.Close();
            }
            catch { };
        }


        
    }
}
